package com.car.bag;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;


import com.wzsck.util.DBManager;
import com.wzsck.util.QueryDataResult;



public class CarManager {
	
	public ArrayList<car> query(int page, int limit,String namecarlei,String namecar) {
		ArrayList<car> list = new ArrayList<car>();
		String sql = "select * from wzs_class,wzs_car  where wzs_class.classid = wzs_car.classnameint";
		if (page != 0 && limit != 0) {
			sql += " limit " + (page - 1) * limit + "," + limit;
		}    //两表联查entry和category    表entry.列categoryId = 表category.列id
		boolean f=false;
		if(namecarlei!=null&&namecarlei.equals("")) {
			if(f==false) {
				sql+="where wzs_class.classname like'% "+namecarlei+"%'";
			}else {
				sql+="and wzs_class.classname like'% "+namecarlei+"%'";
			}
			f=true;
		}
		if(namecar!=null&&namecar.equals("")) {
			if(f==false) {
				sql+="where wzs_class.classname like'%"+namecar+"%'";
			}else {
				sql+="and wzs_class.classname like'%"+namecar+"%'";
			}
			f=true;
		}
		
		
		
		
		QueryDataResult qdr = DBManager.executeQuery(sql);
		if (qdr.rs != null) {
			try {
				while (qdr.rs.next()) {
					int classid = qdr.rs.getInt("classid");
					String classname=qdr.rs.getString("classname");
					int id = qdr.rs.getInt("id");
					int classnameint = qdr.rs.getInt("classnameint");			
					String name = qdr.rs.getString("name");
					String imgurl=qdr.rs.getString("imgurl");
					String describe=qdr.rs.getString("describe");
					int price = qdr.rs.getInt("price");		
					car b = new car(classid,classname,id,classnameint,name,imgurl,describe,price );
					list.add(b);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		qdr.close();
		return list;
	}
	//============================
	//add
			public int add( String carclassname,String name,String price, String describe,String imgurl) {
				String sql = String.format(
						"insert into wzs_car(`classnameint`,`name`,`imgurl`,`describe`,`price`) values('%s','%s','%s','%s',%s)",carclassname,name, imgurl, describe,  price);
				
				System.out.print(sql);
				int i = DBManager.executeUpdate(sql);
				return i;

			}
			
		
			public int update(String id,String classnameint, String name, String imgurl,String describe,String price) {
				String sql = String.format(
						" update wzs_car set `classnameint`='%s',`name`='%s',`imgurl`='%s',`describe`='%s' ,`price` = '%s' where id = %s ",
						classnameint, name, imgurl, describe, price,  id);
				System.out.print(sql);
				int i = DBManager.executeUpdate(sql);
				return i;
			}
			public int del(int id) {
				String sql = "delete from wzs_car where id = " + id;
				int i = DBManager.executeUpdate(sql);
				return i;
			}
	//===============================================================		
	 public int count(String namecarlei,String namecar) {//count查非空
	 		String sql="select count(*) from wzs_car";//换表名
	 		boolean f=false;
			if(namecarlei!=null&&namecarlei.equals("")) {
				if(f==false) {
					sql+="where wzs_class.classname like'% "+namecarlei+"%'";
				}else {
					sql+="and wzs_class.classname like'% "+namecarlei+"%'";
				}
				f=true;
			}
			if(namecar!=null&&namecar.equals("")) {
				if(f==false) {
					sql+="where wzs_class.classname like'%"+namecar+"%'";
				}else {
					sql+="and wzs_class.classname like'%"+namecar+"%'";
				}
				f=true;
			}
	 		QueryDataResult qdr=DBManager.executeQuery(sql);
	 		if (qdr.rs!=null) {
	 			try {
	 				while (qdr.rs.next()) {
	 					int count =qdr.rs.getInt("count(*)");
	 					return count;
	 				}
	 			} catch (SQLException e) {
	 				// TODO Auto-generated catch block
	 				e.printStackTrace();
	 			}
	 		}
	 		return 0;
	 	}
	 
	 
}
